<?php

namespace App\Admin\Controllers;

use App\Models\Commodity;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Commoditytype;
use App\Models\Labels;

class CommodityController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Commodity);

        $grid->column('id', __('Id'));
        $grid->column('name', __('名称'));
        // $grid->column('computing', __('单台算力'));
        // $grid->column('power_consumption', __('功耗'));
        //$grid->column('power_ratio', __('功耗比'));
        $grid->column('price', __('商品价格'));
        // $grid->column('electricity_fee', __('每T算力的电费'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));
        $grid->column('status', __('状态'))->display(function ($status) {
            return config('conf.status')[$status];
        });
        $grid->column('stock', __('商品价格'));
        $grid->column('sold', __('销售量'));



        // $grid->column('computing_stock', __('剩余算力数量'));
        // $grid->column('sell_computing', __('卖出算力'));
        //  $grid->column('income', __('每T算力的收益'));
        $grid->column('sort', __('排序'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Commodity::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        // $show->field('computing', __('Computing'));
        // $show->field('power_consumption', __('Power consumption'));
        //$show->field('power_ratio', __('Power ratio'));
        $show->field('price', __('Price'));
        $show->field('stock', __('stock'));
        $show->field('original', __('original'));
        $show->field('sold', __('sold'));
        //$show->field('electricity_fee', __('Electricity fee'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('status', __('Status'));
        //$show->field('computing_stock', __('Computing stock'));
        // $show->field('sell_computing', __('Sell computing'));
        //   $show->field('income', __('Income'));
        $show->field('sort', __('Sort'));
        $show->field('type', '商品类型id');
        $show->field('rule', '商品描述');
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Commodity);

        $form->text('name', __('商品名称'));

        $form->image('img', __('缩略图'))->uniqueName()->removable();
        $form->multipleImage('thumb','商品图集【可传多个】')->uniqueName()->removable();
        // $form->text('computing', __('单台算力'));
        //$form->text('power_consumption', __('功耗')
        $form->decimal('price', __('商品价格'));
        $form->decimal('original', __('商品原价格'));
        $form->decimal('stock', __('库存'));
        $form->decimal('sold', __('销售量'));
        // $form->decimal('electricity_fee', __('每T算力的电费'));
        $data=Commoditytype::all()->toArray();
        foreach ($data as $item){
            $_item=$item["Id"];
            $_item1=$item["name"];
            $arr[$_item]=$_item1;
        }
        $datas=Labels::all()->toArray();
        foreach ($datas as $items){
            $_item1s=$items["Id"];
            $_item1s=$items["name"];
            $arrs[$_item]=$_item1s;
        }

        $form->select('reserve', __('是否参与分销'))->options(array('1'=>'分销','0'=>'非分销'));
        $form->select('reserve', __('是否预定'))->options(array('1'=>'预定','0'=>'非预定'));
        $form->select('label', __('标签'))->options($arrs);
        $form->select('type', __('类型'))->options($arr);
        $form->select('status', __('状态'))->options(config('conf.status'));
        // $form->number('computing_stock', __('剩余算力'));
        // $form->number('sell_computing', __('卖出算力'));
        // $form->decimal('income', __('每T算力的收益'));
        $form->number('sort', __('排序'));
        //$form->number('cycle', __('周期'));
        $form->ueditor('rule', '商品描述');
        return $form;
    }
}
